var reqHelp = require('../../utils/req.js');
import Toast from '@vant/weapp/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        idCardUrl: '',
        btnShow: false,
        ctx: null,
        duration: 0,
        timer: null,
        realNameGuid: '0',
        readNum: '----',
        startBtnShow: true,
        // showLoading:false
    },

    onLoad(options) {
        var that = this;
        var realNameGuid = options.realNameGuid; //
        that.setData({
            realNameGuid: realNameGuid,
            ctx: wx.createCameraContext(),
        })
        this.listen();
        reqHelp.requestByJson('Mini/RealName.ashx?action=readNum', {
            realNameGuid: realNameGuid,
        }).then(function (res) {
            // console.log(result);
            // var res = JSON.parse(result);
            if (res.getNum == 0) {
                that.setData({
                    readNum: res.data,
                });
            } else {
                that.setData({
                    readNum: res.getMsg,
                });
            }
        });
    },
    listen() {
        const context = this.data.ctx
        const listener = context.onCameraFrame((frame) => {
            // console.log(frame.data instanceof ArrayBuffer, frame.width, frame.height)
        })
        this.setData({
            listener: listener
        })
    },
    //创建
    startRecord() {
        this.setData({
            startBtnShow: false
        })
        this.countTime()
        const context = this.data.ctx
        this.data.listener.start()
        context.startRecord({
            success: (res) => {
                // console.log(res)
            }
        })
    },
    countTime() {
        var that = this;
        this.data.timer = setInterval(() => {
            const num = that.data.duration + 1
            that.setData({
                duration: num
            })
        }, 1000)
    },
    stopRecord() {
        var that = this;
        clearInterval(this.data.timer)
        // this.setData({showLoading:true})
        this.data.ctx.stopRecord({
            success: (stopRes) => {
                this.data.listener.stop();
                const tempFilePath = stopRes.tempVideoPath;
                Toast.loading({
                    duration: 0, 
                    message: '上传中..',
                });
                reqHelp.requestByUpload('Mini/RealName.ashx?action=videoAuth', {
                    realNameGuid: that.data.realNameGuid,
                }, tempFilePath).then(function (result) {
                    Toast.clear();
                    var res = JSON.parse(result);
                    // that.setData({showLoading:false})
                    if (res.getNum == 0) {
                        Toast({
                            type: 'success',
                            message: '实名成功',
                            duration: 3000,
                            onClose: () => {
                                wx.navigateBack({
                                    delta: 4
                                })
                            },
                        });
                    } else {
                        Toast({
                            type: 'text',
                            message: res.getMsg,
                            duration: 3500,
                        });
                    }
                })
            }
        })
    },
    onCancel() {
        var that = this
        wx.navigateTo({
            url: 'uploadVideo?realNameGuid=' + that.data.realNameGuid,
        })
    }
})